import { getActivityRegistrations, updateRegistrationStatus } from '../../api/registration'

// 报名模块
export default {
  namespaced: true,
  state: {
    // 参与者列表
    participants: [],
    // 当前活动ID
    currentActivityId: null,
    // 加载状态
    loading: false,
    // 错误信息
    error: null
  },
  
  mutations: {
    // 设置参与者列表
    SET_PARTICIPANTS(state, participants) {
      state.participants = participants
    },
    
    // 设置当前活动ID
    SET_CURRENT_ACTIVITY_ID(state, activityId) {
      state.currentActivityId = activityId
    },
    
    // 设置加载状态
    SET_LOADING(state, loading) {
      state.loading = loading
    },
    
    // 设置错误信息
    SET_ERROR(state, error) {
      state.error = error
    },
    
    // 更新参与者状态
    UPDATE_PARTICIPANT_STATUS(state, { registrationId, status }) {
      const participant = state.participants.find(p => p.id === registrationId)
      if (participant) {
        participant.status = status
      }
    }
  },
  
  actions: {
    // 获取活动参与者列表
    async getActivityRegistrations({ commit, rootDispatch }, activityId) {
      try {
        rootDispatch('showLoading')
        commit('SET_CURRENT_ACTIVITY_ID', activityId)
        const response = await getActivityRegistrations({ activityId })
        commit('SET_PARTICIPANTS', response.data || [])
        return response.data || []
      } catch (error) {
        const errorMsg = error.response?.data?.message || '获取参与者列表失败'
        commit('SET_ERROR', errorMsg)
        rootDispatch('showError', errorMsg)
        return []
      } finally {
        rootDispatch('hideLoading')
      }
    },
    
    // 同意报名
    async approveRegistration({ commit, rootDispatch }, registrationId) {
      try {
        await updateRegistrationStatus(registrationId, { status: 'APPROVED' })
        commit('UPDATE_PARTICIPANT_STATUS', { registrationId, status: 'APPROVED' })
        rootDispatch('showSuccess', '已同意报名')
      } catch (error) {
        const errorMsg = error.response?.data?.message || '操作失败'
        rootDispatch('showError', errorMsg)
        throw error
      }
    },
    
    // 拒绝报名
    async rejectRegistration({ commit, rootDispatch }, registrationId) {
      try {
        await updateRegistrationStatus(registrationId, { status: 'REJECTED' })
        commit('UPDATE_PARTICIPANT_STATUS', { registrationId, status: 'REJECTED' })
        rootDispatch('showSuccess', '已拒绝报名')
      } catch (error) {
        const errorMsg = error.response?.data?.message || '操作失败'
        rootDispatch('showError', errorMsg)
        throw error
      }
    },
    
    // 清空参与者数据
    clearParticipants({ commit }) {
      commit('SET_PARTICIPANTS', [])
      commit('SET_CURRENT_ACTIVITY_ID', null)
    }
  },
  
  getters: {
    // 获取参与者列表
    participants: state => state.participants,
    
    // 获取当前活动ID
    currentActivityId: state => state.currentActivityId,
    
    // 获取加载状态
    isLoading: state => state.loading,
    
    // 获取错误信息
    error: state => state.error,
    
    // 根据ID获取参与者
    getParticipantById: state => id => {
      return state.participants.find(participant => participant.id === id)
    }
  }
}